package com.fhj.bookshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fhj.bookshop.entity.Favourite;
import com.fhj.bookshop.entity.Product;
import com.fhj.bookshop.mapper.FavouriteMapper;
import com.fhj.bookshop.service.IFavouriteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fu
 * @since 2023-12-24
 */
@Service
public class FavouriteServiceImpl extends ServiceImpl<FavouriteMapper, Favourite> implements IFavouriteService {
    @Autowired
    FavouriteMapper favouriteMapper;

    @Override
    public void addFavourite(Favourite favourite) {
        favouriteMapper.insert(favourite);
    }

    @Override
    public void removeFavourite(Integer custId, Integer proId) {
        LambdaQueryWrapper<Favourite> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Favourite::getCustId,custId);
        wrapper.eq(Favourite::getProId,proId);
        favouriteMapper.delete(wrapper);
    }

    @Override
    public Integer checkFavourite(Integer custId, Integer proId) {
        LambdaQueryWrapper<Favourite> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Favourite::getCustId,custId);
        wrapper.eq(Favourite::getProId,proId);
        Favourite favourite=favouriteMapper.selectOne(wrapper);
        if(favourite==null) {
            return 0;
        }else{
            return 1;
        }
    }

    @Override
    public List<Favourite> findByCustId(Integer custId) {
        LambdaQueryWrapper<Favourite> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Favourite::getCustId,custId);
        List<Favourite> favourites=favouriteMapper.selectList(wrapper);
        return favourites;
    }
}
